import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'

// 引入 vue-router
import { createRouter, createWebHistory } from 'vue-router'
// 引入 element-plus
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// 暗黑模式
import 'element-plus/theme-chalk/dark/css-vars.css'
// 引入axios
import axios from 'axios'



// 示例路由配置
import PdfViewer from './views/PdfViewer.vue'
import Login from './views/Login.vue'
import Home from './views/Home.vue'
import List from './views/CompareIndex.vue'
import Compare from './views/Compare.vue'
import ComparePdf from './views/ComparePdf.vue'

const routes = [
  { path: '/', component: Login, name: 'Login' },
  { path: '/login', component: Login, name: 'Login' },
  { path: '/home', component: Home, name: 'Home' },
  { path: '/list', component: List, name: 'List' },
  { path: '/compare', component: Compare, name: 'Compare' },
  { path: '/comparePdf', component: ComparePdf, name: 'ComparePdf' },
  { path: '/pdf', component: PdfViewer, name: 'PdfViewer' },
  // 你可以在这里添加更多页面
  { path: '/:pathMatch(.*)*', redirect: '/login' }, // 未匹配到的路由重定向到login
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  // to: 从哪个页面
  // from: 到哪个页面
  // next: 只有执行next()页面
 
  //判断用户是否登录
  console.log(localStorage.getItem('token'))
  if(!localStorage.getItem('token')){
    // 未登录，跳转到login页面
    if( to.path === '/login'){
      next()
      return
    }
    next('/login')
  }else{
    // 已登录
    next()
  }
})


const app = createApp(App)
// 全局注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(router)
app.use(ElementPlus, {
  locale: zhCn
})
app.mount('#app')
